<?php
session_start();
require('../conn.php');
require('../functions.php');
require('admin.php');
$page = $_GET['page'];
if (is_numeric($page) == false) {
    $page = 1;
    $page_1 = 1;
    $min = 0;
    $page_2 = $page + 1;
} else {
    if ($page > 1) {
        $page_2 = $page + 1;
        $min = ($page - 1) * 35;
        $page_1 = $page - 1;
    } else {
        $page_1 = 1;
        $min = 0;
        $page_2 = 2;
    }
}
$max = $page * 35;
$sql = "select * from `user`  order by `id` desc limit {$min},{$max}";
//永久封号,ban置为-1
$del_id = guolv($_GET['del_id']);
if (is_numeric($del_id)) {
    $mysql->query("update `user` set `ban` = -1  where `id`={$del_id}");
    echo '<script>alert(\'封号成功\');location.href=\'user.php\'</script>';
    exit;
}
//禁言
if($_GET['action'] == 'ban'){
    $uid = $_GET['uid'];
    $day = date("Y-m-d",time());
    $res = $mysql->execute("update `user` set `ban` = 1,`ban_day` = '{$day}' WHERE `id` = {$uid}");
    if($res){
        echo "<script>alert('禁言成功')</script>";
        echo "<script>location.href='./user.php'</script>";
    }
}
if ($_GET['do'] == 'search') {
    $phone = guolv($_GET['phone']);
    if (substr($phone, 0, 1) == '#') {
        $search_id = str_replace('#', '', $phone);
        $sql = "select * from `user` where `id`='{$search_id}' order by `id` desc limit {$min},{$max}";
    }else if(substr($phone, 0, 1) == '@'){
        $search_id = str_replace('@', '', $phone);
        $sql = "select * from `user` where `phone`='{$search_id}' order by `id` desc limit {$min},{$max}";
    } else {
        $sql = "select * from `user` where `name` LIKE '%{$phone}%' order by `id` desc limit {$min},{$max}";
    }
}
if($_GET['do'] == 'money'){
    $sql = "select * from `user` order by `total` desc limit {$min},{$max}";
}
if($_GET['do'] == 'gold'){
    $sql = "select * from `user` order by `gold` desc limit {$min},{$max}";
}
if($_GET['do'] == 'student'){
    $sql = "Select * from user
left join (SELECT pid,COUNT(*) as Count1 from user where pid != 0 GROUP BY pid) T2
on user.id=T2.pid order by T2.Count1 desc limit {$min},{$max}";
}
echo $sql;
$row_user = $mysql->query($sql);
?>

<?php include('head.php') ?>
<div class="container-fluid">
    <div class="row-fluid">
        <?php
        include('left.php'); ?>
        <div class="span10">
            <h1 class="page-title">用户管理</h1>
            <div class="well">
                <div id="myTabContent" class="tab-content">
                    <div class="tab-pane active in" id="home">
                        <div class="well">
                            <form class="navbar-search pull-left" action="user.php" method="get">
                                <input type="hidden" name="do" value="search">
                                <input type="text" class="search-query" placeholder="#+id搜索会员" name="phone">
                            </form>
                            <div class="pull-right">
                                <a href="user.php" class="btn">首页</a></button>
                                <a href="user.php?
                                <?php
                                    if($_GET['do'] == 'money'){
                                    echo "do=money&";
                                     }else if($_GET['do'] == 'student'){
                                        echo "do=student&";
                                    }else if($_GET['do'] == 'gold'){
                                        echo "do=gold&";
                                    }
                                ?>page=<?php echo $page_1 ?>" class="btn"
                                   style="margin-left:5px;">上一页</a>
                                <a href="user.php?<?php if($_GET['do'] == 'money'){
                                    echo "do=money&";
                                }else if($_GET['do'] == 'student'){
                                    echo "do=student&";
                                }else if($_GET['do'] == 'gold'){
                                    echo "do=gold&";
                                }?>page=<?php echo $page_2 ?>" class="btn"
                                   style="margin-left:5px;">下一页</a>
                            </div>
                            <table class="table">
                                <thead>
                                <tr>
                                    <th>#</th>
                                    <th>手机号</th>
                                    <th>昵称</th>
                                    <th>注册时间</th>
                                    <th><a href="?do=money">余积分</a></th>
                                    <th><a href="?do=gold">余金币</th>
                                    <th>已提现</th>
                                    <th>师傅ID</th>
                                    <th><a href="?do=student">总徒弟数</th>
                                    <th>有收徒徒弟数</th>
                                    <th>徒孙数</th>
                                    <th>状态</th>
                                    <th>操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <?php
                                foreach ($row_user as $user) {
                                    if ($user['ban'] == 0) {
                                        $ban = "<font color='green'>正常</font>";
                                    }else if($user['ban'] == 1){
                                        $ban = "<font color='blue'>禁言</font>";
                                    }else if($user['ban'] == -1){
                                        $ban = "<font color='red'>封号</font>";
                                    }
                                    //总徒弟数
                                    $studentNum = $mysql->query("select count(*) as num from `user` WHERE `pid`={$user['id']}");
                                    //有收徒徒弟数
                                    $ssdtudentNum = $mysql->query("select count(DISTINCT(pid)) as num from `user` where `pid` in (SELECT `id` from `user` where `pid`={$user['id']}) ");
                                    //徒孙数
                                    $ssstudentNum = $mysql->query("select count(*) as num from `user` where `pid` in (SELECT `id` from `user` where `pid`={$user['id']}) ");
                                    $txmoney = $mysql->query("select sum(money) as money from `txdata` WHERE `uid`={$user['id']} AND `state`=2");
                                    print <<<table
        <tr>
          <td><a target="_blank"  href="http://app.weiyouqu.net/weiApp/user_detail.php?id={$user['id']}" title="查看用户明细">{$user['id']}</a></td>
     	  <td>{$user['phone']}</td>
     	  <td>{$user['name']}</td>
     	  <td>{$user['day']}</td>
		  <td>{$user['total']}</td>
		  <td>{$user['gold']}</td>
		  <td>{$txmoney[0]['money']}</td>
		  <td>{$user['pid']}</td>
		  <td><a target="_blank" href="./students.php?id={$user['id']}" title="徒弟列表">{$studentNum[0]['num']}</a></td>
		  <td>{$ssdtudentNum[0]['num']}</td>
		  <td>{$ssstudentNum[0]['num']}</td>
		  <td>{$ban}</td>
		  <td>
			<a class="btn" style="color:red" title="封号" onclick="del_id({$user['id']})">封号</a>
			<a class="btn" style="color:green" title="编辑" href="javascript:if(confirm('确认禁言？')){window.location.href = './user.php?uid={$user['id']}&action=ban';}">禁言</a>
			<a class="btn" target="_blank"style="color:green" title="编辑" href="./edit_user.php?uid={$user['id']}">编辑</a>
		  </td>
        </tr>
table;
                                } ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <div class="tab-pane fade" id="profile">
                    </div>
                </div>

            </div>

        </div>
    </div>


</div>
<script src="lib/bootstrap/js/bootstrap.js"></script>
<script language="JavaScript">
    function del_id(id) {
        if (confirm("确认封号？")) {
            location.href = "?del_id=" + id;
        } else {
            return false;
        }
    }
</script>


</body>
</html>